﻿using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("MeshRenderer/Skin/SkinToParent",
           "Converts MeshRenders into Skinned Meshes."
            , 0)]
        public static void MeshRendersSkinToParent()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<MeshRenderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (SkinnEx.IsNullOrNotVailid(context)) continue;
                SkinnedMeshRenderer skinned; 
                if (!context.CloneSkinnedTo(out skinned, context.transform.parent)) continue;

                SkinnContext.UndoRecord(context, "Skin To Parent");
                context.gameObject.SetActive(false);
                Undo.RegisterCreatedObjectUndo(skinned, "Skin To Parent");
            }
            SkinnContext.Finished();
        }
    }
}